package com.supor.aiot.ui.privacy;

import android.view.View;
import android.widget.ImageView;

import com.supor.aiot.R;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.base.NoViewModel;
import com.supor.aiot.databinding.ActivitySplashBinding;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import static androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: 引导页 <br>
 * 作者: lei <br>
 * 日期: 2020/1/23 <br>
 */
public class SplashActivity extends BaseMVVMActivity<NoViewModel, ActivitySplashBinding> {

    private List<Fragment> fragmentList;
    private int currentIndicator = 0;
    private ImageView[] imageViews;

    @Override
    protected int resourceId() {
        return R.layout.activity_splash;
    }

    @Override
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    public void initData() {
        fragmentList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            fragmentList.add(SplashFragment.newInstance(i + 1));
        }
        bindingView.viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        int len = fragmentList.size();
        imageViews = new ImageView[len];
        for (int i = 0; i < len; i++) {
            imageViews[i] = (ImageView) bindingView.llIndicator.getChildAt(i);
            imageViews[i].setBackgroundResource(R.drawable.image_indicator_normal);
        }
        imageViews[currentIndicator].setBackgroundResource(R.drawable.image_indicator_selected);
        bindingView.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                imageViews[position].setBackgroundResource(R.drawable.image_indicator_selected);
                imageViews[currentIndicator].setBackgroundResource(R.drawable.image_indicator_normal);
                currentIndicator = position;
                if (position == 3) {
                    bindingView.llIndicator.setVisibility(View.GONE);
                } else {
                    bindingView.llIndicator.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(0);
        finish();

    }
}
